/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.DriverHandler;
import control.TaskHandler;
import static java.awt.image.ImageObserver.PROPERTIES;
import model.Driver;
import model.Task;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Jeppe
 */
public class Oversigt extends javax.swing.JPanel {

    private Main main;
    private TaskHandler taskHandler;
    private DriverHandler driverHandler;
    private String searchType;
    private boolean firstTimeRun;

    /**
     * Creates new form SøgOpgaver
     */
    public Oversigt(Main main) {
        initComponents();
        this.main = main;
        this.taskHandler = main.getTaskHandler();
        this.driverHandler = main.getDriverHandler();
        searchType = "Dato";
        firstTimeRun = true;

    }

    public void searchFunction() {
        if (taskSearchRadio.isSelected()) {
            if (searchType.equals("searchField")) {
                if (Søgtxt.getText().matches("\\d+")) {
                    søgResultatList.setListData(taskHandler.getTaskListByPhoneNo(Integer.parseInt(Søgtxt.getText())).toArray());
                    if (taskHandler.getTaskListByPhoneNo(Integer.parseInt(Søgtxt.getText())).size() >= 1) {
                        messageLabel.setText("Valgte Opgave:");
                    } else {
                        messageLabel.setText("Ingen opgaver fundet på tlf: " + Søgtxt.getText());
                        søgResultatList.setListData(taskHandler.getTaskListByDriverPhone(Integer.parseInt(Søgtxt.getText())).toArray());
                        if(taskHandler.getTaskListByDriverPhone(Integer.parseInt(Søgtxt.getText())).size()>=1){
                            messageLabel.setText("Valgte opgave:");
                        }else{
                            messageLabel.setText("Ingen opgaver fundet på tlf: " + Søgtxt.getText());
                        }
                    }
                }else if(Søgtxt.getText().equals("")){
                    messageLabel.setText("Du har ikke søgt på noget endnu..");
                }
            } else if (searchType.equals("Dato")) {
//                System.out.println("Der er fundet " + taskHandler.getTaskListByDato(jCalendar1.getDate().getDate(), jCalendar1.getDate().getMonth() + 1, jCalendar1.getDate().getYear() + 1900).size() + " tasks på dato: " + jCalendar1.getDate());
                søgResultatList.setListData(taskHandler.getTaskListByDato(jCalendar1.getDate().getDate(), jCalendar1.getDate().getMonth() + 1, jCalendar1.getDate().getYear() + 1900).toArray());
                if(taskHandler.getTaskListByDato(jCalendar1.getDate().getDate(), jCalendar1.getDate().getMonth() + 1, jCalendar1.getDate().getYear() + 1900).size() >= 1){
                    messageLabel.setText("");
                }else{
                    messageLabel.setText("Ingen opgaver fundet på dato: "+jCalendar1.getDate().getDate()+"/"+jCalendar1.getDate().getMonth() + 1+"-"+ jCalendar1.getDate().getYear());
                }
            }
        } else if (driverSearchRadio.isSelected()) {
            if (Søgtxt.getText().matches("\\d+")) {
                if (driverHandler.getDriver(Integer.parseInt(Søgtxt.getText())) != null) {
                    resultatArea.setText(driverHandler.getDriver(Integer.parseInt(Søgtxt.getText())).toStringInfo());
                    søgResultatList.removeAll();
                } else {
                    resultatArea.setText("Der er ingen chauffører i databasen med tlfnr: " + Søgtxt.getText());
                }
            } else {
                if (driverHandler.getDriverListByName(Søgtxt.getText()).size() >= 1) {
                    søgResultatList.setListData(driverHandler.getDriverListByName(Søgtxt.getText()).toArray());
                } else {
                    resultatArea.setText("Kunne ikke finde nogen chauffører der minder om " + Søgtxt.getText());
                }
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        Søgtxt = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        søgResultatList = new javax.swing.JList();
        jCalendar1 = new com.toedter.calendar.JCalendar();
        taskSearchRadio = new javax.swing.JRadioButton();
        driverSearchRadio = new javax.swing.JRadioButton();
        jPanel4 = new javax.swing.JPanel();
        messageLabel = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jScrollPane2 = new javax.swing.JScrollPane();
        resultatArea = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        tilbageKnap = new javax.swing.JButton();
        Baggrund = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(1070, 632));
        setPreferredSize(new java.awt.Dimension(1070, 632));
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, -20, 10, 620));

        jPanel1.setOpaque(false);

        Søgtxt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                SøgtxtKeyReleased(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Søgefelt:");

        søgResultatList.setBackground(new java.awt.Color(204, 204, 204));
        søgResultatList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                søgResultatListValueChanged(evt);
            }
        });
        søgResultatList.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                søgResultatListPropertyChange(evt);
            }
        });
        jScrollPane1.setViewportView(søgResultatList);

        jCalendar1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jCalendar1PropertyChange(evt);
            }
        });

        buttonGroup1.add(taskSearchRadio);
        taskSearchRadio.setForeground(new java.awt.Color(255, 255, 255));
        taskSearchRadio.setSelected(true);
        taskSearchRadio.setText("Opgave");
        taskSearchRadio.setContentAreaFilled(false);
        taskSearchRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taskSearchRadioActionPerformed(evt);
            }
        });

        buttonGroup1.add(driverSearchRadio);
        driverSearchRadio.setForeground(new java.awt.Color(255, 255, 255));
        driverSearchRadio.setText("Chauffør");
        driverSearchRadio.setContentAreaFilled(false);
        driverSearchRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                driverSearchRadioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(taskSearchRadio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(driverSearchRadio))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(Søgtxt))
                    .addComponent(jCalendar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE))
                .addContainerGap(13, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(31, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Søgtxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(15, 15, 15)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(taskSearchRadio)
                    .addComponent(driverSearchRadio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCalendar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 50, 350, 540));

        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
        jPanel4.setOpaque(false);

        messageLabel.setFont(new java.awt.Font("Garamond", 1, 24)); // NOI18N
        messageLabel.setForeground(new java.awt.Color(255, 255, 255));
        messageLabel.setText("Du har ikke søgt på noget endnu..");

        jScrollPane2.setOpaque(false);

        resultatArea.setEditable(false);
        resultatArea.setColumns(20);
        resultatArea.setRows(5);
        jScrollPane2.setViewportView(resultatArea);

        jButton1.setText("Slet Opgave");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 479, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 608, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel4Layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(messageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 540, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(messageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 492, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(27, 27, 27))))
        );

        add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 10, 610, 580));

        tilbageKnap.setForeground(new java.awt.Color(255, 255, 255));
        tilbageKnap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/backbutton.png"))); // NOI18N
        tilbageKnap.setContentAreaFilled(false);
        tilbageKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tilbageKnapActionPerformed(evt);
            }
        });
        add(tilbageKnap, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 50, 30));

        Baggrund.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/Baggrund 2.PNG"))); // NOI18N
        add(Baggrund, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1020, 600));
    }// </editor-fold>//GEN-END:initComponents

    private void tilbageKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tilbageKnapActionPerformed
        main.visStartPanel();
    }//GEN-LAST:event_tilbageKnapActionPerformed

    private void søgResultatListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_søgResultatListValueChanged
        resultatArea.setText("");
        if (søgResultatList.getSelectedIndex() >= 0) {
            if (driverSearchRadio.isSelected()) {
                Driver driver = (Driver) søgResultatList.getSelectedValue();
                resultatArea.setText(driver.toStringInfo());
                messageLabel.setText("Valgte Chauffør: ");
            } else if (taskSearchRadio.isSelected()) {
                Task task = (Task) søgResultatList.getSelectedValue();
                resultatArea.setText(task.toStringInfo());
                messageLabel.setText("Valgte Opgave: ");
            }
        }
    }//GEN-LAST:event_søgResultatListValueChanged

    private void SøgtxtKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_SøgtxtKeyReleased
        resultatArea.setText("");
        messageLabel.setText("");
        searchType = "searchField";
        searchFunction();
    }//GEN-LAST:event_SøgtxtKeyReleased

    private void jCalendar1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jCalendar1PropertyChange
        searchType = "Dato";
        if (!firstTimeRun) {
            searchFunction();
        }
    }//GEN-LAST:event_jCalendar1PropertyChange

    private void driverSearchRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_driverSearchRadioActionPerformed
        resultatArea.setText("");
        if (driverSearchRadio.isSelected()) {
            jCalendar1.setEnabled(false);
        }
        searchFunction();
    }//GEN-LAST:event_driverSearchRadioActionPerformed

    private void taskSearchRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taskSearchRadioActionPerformed
        resultatArea.setText("");
        if (taskSearchRadio.isSelected()) {
            jCalendar1.setEnabled(true);
        }
        searchFunction();
    }//GEN-LAST:event_taskSearchRadioActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        firstTimeRun = false;
        searchFunction();
        main.setSize(1038, 670);
    }//GEN-LAST:event_formComponentShown

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int valg = JOptionPane.showConfirmDialog(this, "Er du sikker på du vil slette den valgte opgave?", "Bekræft", 1);
        if(valg==0){
            taskHandler.delete((Task) søgResultatList.getSelectedValue());
            JOptionPane.showMessageDialog(this, "Opgaven er nu slettet", "Information", 3);
        }
        
        searchFunction();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void søgResultatListPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_søgResultatListPropertyChange
//        resultatArea.setText("");
//        if (søgResultatList.getSelectedIndex() >= 0) {
//            if (driverSearchRadio.isSelected()) {
//                Driver driver = (Driver) søgResultatList.getSelectedValue();
//                resultatArea.setText(driver.toStringInfo());
//            } else if (taskSearchRadio.isSelected()&& søgResultatList.getSelectedValue()!= null) {
//                Task task = (Task) søgResultatList.getSelectedValue();
//                resultatArea.setText(task.toStringInfo());
//            }
//        }
    }//GEN-LAST:event_søgResultatListPropertyChange

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Baggrund;
    private javax.swing.JTextField Søgtxt;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButton driverSearchRadio;
    private javax.swing.JButton jButton1;
    private com.toedter.calendar.JCalendar jCalendar1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JTextArea resultatArea;
    private javax.swing.JList søgResultatList;
    private javax.swing.JRadioButton taskSearchRadio;
    private javax.swing.JButton tilbageKnap;
    // End of variables declaration//GEN-END:variables
}
